import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import folium
import folium.plugins
import io
from matplotlib import animation,rc
import base64
data=pd.read_csv("globalterrorismdb.csv",encoding='ISO-8859-1')
data
data_new = data.dropna(thresh=160000,axis=1)
data_new
data_new.shape
data_new.isnull().sum()
data_new['casualities']=data_new['nkill']+data_new['nwound']
print('Country with Highest Terrorist Attacks:',data_new['country_txt'].value_counts().index[0])
print('Regions with Highest Terrorist Attacks:',data_new['region_txt'].value_counts().index[0])
print('Maximum people killed in an attack are:',data_new['nkill'].max(),'that took place in',data_new.loc[data_new['nkill'].idxmax()].country_txt)
print("Year with the most attacks:",data_new['iyear'].value_counts().idxmax())
print("Month with the most attacks:",data_new['imonth'].value_counts().idxmax())
print("Most Attack Types:",data_new['attacktype1_txt'].value_counts().idxmax())
sns.barplot(data_new['gname'].value_counts()[1:10].values,data_new['gname'].value_counts()[1:10].index,palette='Set1')
plt.xticks(rotation=90)
fig=plt.gcf()
fig.set_size_inches(10,8)
plt.title('Terrorist Groups with Highest Terror Attacks')
plt.show()
print(f"The highest terrorist attacks were commited in {data_new.country_txt.value_counts().index[0]} with {data_new.country.value_counts().max()} attacks")
print('\nThe other 9 countries with highest terrorist attacks are:')
for i in range(1,10):
print(f"{i+1}. {data_new.country_txt.value_counts().index[i]} with {data_new.country_txt.value_counts()[i]} attacks")
#Visualization
plt.subplots(figsize=(15,6))
sns.barplot(data_new['country_txt'].value_counts()[:10].index,data_new['country_txt'].value_counts()[:10].values,palette='Set1')
plt.title('Top Countries Affected')
plt.xlabel('Countries')
plt.ylabel('Count')
plt.xticks(rotation= 90)
plt.show()
f, ax = plt.subplots(figsize=(10, 7))
plt.title('Number Of Terrorist Activities per Year')
sns.despine(f)
sns.distplot(data_new['iyear'], bins=20,color="g")
pd.crosstab(data_new.iyear, data_new.region_txt).plot(kind='area',figsize=(15,6))
plt.title('Terrorist Activities by Region each Year')
plt.ylabel('Number of Attacks')
plt.show()
plt.subplots(figsize=(10,7))
year_casual = data_new.groupby('iyear').casualities.sum().to_frame().reset_index()
year_casual.columns = ['Year','Casualities']
plt.title('Number Of Casualities Each Year')
sns.lineplot(x='Year', y='Casualities', data=year_casual,palette="Set2",color="g")
plt.subplots(figsize=(15,6))
sns.countplot(data_new['targtype1_txt'],palette='Set1',order=data_new['targtype1_txt'].value_counts().index)
plt.xticks(rotation=90)
plt.title('Most common target')
plt.show()
terror_fol=data_new.copy()
terror_fol.dropna(subset=['latitude','longitude'],inplace=True)
location_fol=terror_fol[['latitude','longitude']][:8000]
country_fol=terror_fol['country_txt'][:8000]
city_fol=terror_fol['city'][:8000]
killed_fol=terror_fol['nkill'][:8000]
wound_fol=terror_fol['nwound'][:8000]
def color_point(x):
if x>=30:
color='red'
elif ((x>0 and x<30)):
color='blue'
else:
color='orange'
return color
def point_size(x):
if (x>30 and x<100):
size=2
elif (x>=100 and x<500):
size=8
elif x>=500:
size=16
else:
size=0.5
return size
map2 = folium.Map(location=[30,0],tiles='cartodbpositron',zoom_start=2)
for point in location_fol.index:
info='<b>Country: </b>'+str(country_fol[point])+'<br><b>City: </b>: '+str(city_fol[point])+'<br><b>Killed </b>: '+str(killed_fol[point])+'<br><b>Wounded</b> : '+str(wound_fol[point])
iframe = folium.IFrame(html=info, width=200, height=200)
folium.CircleMarker(list(location_fol.loc[point].values),popup=folium.Popup(iframe),radius=point_size(killed_fol[point]),color=color_point(killed_fol[point])).add_to(map2)
map2